#include "register.h"
#include "ui_register.h"
#include "loginregister.h"
#include "database.h"
Register::Register(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Register)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    DatabaseMysql *drr = new DatabaseMysql();
    db = drr->db;
    ui->userName->setPlaceholderText("请输入用户名");
    ui->password->setPlaceholderText("请输入密码");
    ui->password2->setPlaceholderText("请确认密码");
}

Register::~Register()
{
    delete ui;
}

void Register::on_pushButton_clicked()
{
    DatabaseMysql datas;
    this->db=datas.db;
    this->query=datas.query;
    QString account = this->ui->userName->text();
    QString password = this->ui->password->text();
    QString password2 = this->ui->password2->text();
    QSqlQuery result = db.exec(" select * from users");

    while(password!=password2){
        QMessageBox::information(NULL, "提示", "两次密码不一致请重新输入");
        QString password = this->ui->password->text();
        QString password2 = this->ui->password2->text();
        break;
    }

    while(result.next())
    {
        string account = this->ui->userName->text().toStdString();
        string Account = result.value("username").toString().toStdString();
        string Password = result.value("userpassword").toString().toStdString();

        if(account.compare(Account) == 0)
        {
                          QMessageBox::warning(NULL, "提示", "该用户名已经被使用，请重新输入");
                          account.clear();
                          password.clear();
                          password2.clear();
        }
    }
    QString str = QString("INSERT INTO users(username,userpassword,score)VALUES('%1','%2',0)").arg(account).arg(password);
    if(query.exec(str)){
           QMessageBox::information(NULL, "提示", "注册成功！");
           this->close();
           LoginRegister *lr = new LoginRegister;
           lr->show();
    }else{
         QMessageBox::information(NULL, "提示", "注册失败！");
    }
    db.close();

}


void Register::on_btn_back_clicked()
{
    this->hide();
    LoginRegister *lr = new LoginRegister;
    lr->show();
}
